home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu745.dms / pu745.adf / GLOBE099.LHA / Ami-Globe / flag.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  13KB  |  492 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*      fichier         : flag.c                                        */
  4. /*      projet          : amiglobe                                      */
  5. /*      date création   : mai 1994                                      */
  6. /*      commentaire     : dessin des drapeaux                           */
  7. /*      révision        : $VER: flag.c 1.001 (28 Aug 1994)              */
  8. /*      copyright       : Olivier Collard, Thomas Landspurg             */
  9. /*      $HISTORY:                                                */
  10. /*                                                              */
  11. /*                     mai 1994 : 0.01 : version initiale
  12. /*                                                                      */
  13. /************************************************************************/
  14.  
  15.  
  16. /************************************************************************/
  17. /*      includes                                                        */
  18. /************************************************************************/
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <libraries/dos.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include <ctype.h>
  28. #include <math.h>
  29. #include <exec/types.h>
  30. #include <intuition/intuition.h>
  31. #include <intuition/intuitionbase.h>
  32. #include <graphics/gfx.h>
  33. #include <graphics/gfxmacros.h>
  34. #include <graphics/gfxbase.h>
  35. #include <proto/exec.h>
  36. #include <proto/dos.h>
  37. #include <proto/intuition.h>
  38. #include <proto/graphics.h>
  39.  
  40. #include "amiglobe_types.h"
  41. #include "3d_types.h"
  42. #include "3d_protos.h"
  43.  
  44. #define BUFFER_SIZE 300
  45.  
  46. typedef struct Def_Token{
  47.     char *Name;
  48.     int Nb_Min_Arg; /* Nombre minimum d'argument */
  49.     int Nb_Max_Arg; /* Nombre max d'argument (-1 -> illimite) */
  50. };
  51.  
  52.  
  53. typedef enum E_ACTION{
  54.     A_NONE,
  55.     A_END ,
  56.     A_BACKG,
  57.     A_APEN,
  58.     A_OPEN,
  59.     A_RECT,
  60.     A_RECTF,
  61.     A_CIRCLE,
  62.     A_CIRCLEF,
  63.     A_POLY,
  64.     A_POLYF,
  65.     A_ROTATE,
  66.     A_FIVE_STAR,
  67.     A_STAR_MULT,
  68.     A_LAST
  69. };
  70.  
  71. #define MAX_TOKEN   A_LAST
  72.  
  73. struct  Def_Token   Tab_Token[MAX_TOKEN]={
  74. {"NONE"     ,0,0},
  75. {"END"      ,0,0},
  76. {"BACKG"    ,1,3},
  77. {"APEN"     ,1,3},
  78. {"OPEN"     ,1,3},
  79. {"RECT"     ,4,4},
  80. {"RECTF"    ,4,4},
  81. {"CIRCLE"   ,3,4},      /* Si nombre de parametre=3 ->cercle,sinon ellipse */
  82. {"CIRCLEF"  ,3,4},
  83. {"POLY"     ,2,100},
  84. {"POLYF"    ,2,100},
  85. {"ROTATE"   ,1,1},
  86. {"STAR"     ,2,4},      /* posx,posy,[size],[angle] */
  87. {"STARM"    ,5,5}       /* posx,posy,nombre,size_ext,size_int */
  88. };
  89.  
  90.  
  91. typedef struct Def_Color{
  92.     char    *Name;      /* Nom de la couleurs     */
  93.     int r,v,b;      /* Valeurs des composantes*/
  94.     int Num_Col;    /* Numero dans la palette */
  95. };
  96. struct  Def_Color   Tab_Color[]={
  97. { "black"   ,0,0,0      ,0},
  98. { "white"   ,0xff,0xff,0xff ,1},
  99. { "red"     ,0xff,0,0   ,2},
  100. { "green"   ,0,0xff,0   ,3},
  101. { "yellow"  ,0xff,0xff,0    ,4},
  102. { "blue"    ,0x00,0x00,0xff ,5},
  103. { "orange"  ,0xff,0x55,0x00 ,6},
  104. { ""        ,-1,-1,-1   ,-1}
  105. };
  106.  
  107. #define MAX_ELEMS   1000    /* Nombre maximum d'items constituant un drap */
  108.  
  109. int dx,dy;
  110.  
  111. #define FLAG_X  3000    /* Taille du trabeau dans ses propres unitees */
  112. #define FLAG_Y  2000    /* Taille du trabeau dans ses propres unitees */
  113.  
  114. CLIP    glob_clip;
  115.  
  116. void    InitColors(struct ColorMap *cm);
  117.  
  118. /**************************************************************************/
  119.  
  120. int CX(int x)
  121. {
  122.     return( (x*dx)/FLAG_X+glob_clip.minx);
  123. }
  124.  
  125. /**************************************************************************/
  126.  
  127. int CY(int y)
  128. {
  129.     return( (y*dy)/FLAG_Y+glob_clip.miny);
  130. }
  131.  
  132. /**************************************************************************/
  133.  
  134. void Flag_Draw(struct RastPort *rp,CLIP *p_clip,int *P_Tab,struct ColorMap *cm)
  135. {
  136.     int Nb_Arg;
  137.     int i,Token;
  138.     BOOL    Flg_End;
  139.     int rx,ry,px,py,Sx,Sy;
  140.     int Nb,Sext,Sint;
  141.     float   Angle;
  142.  
  143.  
  144.     static  Tab_Star[][2]={
  145.         {-95,30},{-30,30},{0,100},{30,30},{95,30},{40,-0},
  146.         {69,-70},{0,-20},{-69,-70},{-40,-0},{-1,-1}
  147.         };
  148.     glob_clip=*p_clip;
  149.     dx=glob_clip.maxx-glob_clip.minx;
  150.     dy=glob_clip.maxy-glob_clip.miny;
  151.  
  152.     InitColors(cm);
  153.  
  154.     BNDRYOFF(rp);
  155.     Flg_End=FALSE;
  156.     i=0;
  157.     while(Flg_End==FALSE){
  158.         Token=*P_Tab++;
  159.         Nb_Arg=Token>>8;
  160.         Token=Token&0xFF;
  161. #ifdef DEBUG_LEV1
  162.         printf("Token:%d Nb Arg:%d\n",Token,Nb_Arg);
  163. #endif
  164.         switch(Token){
  165.             case A_NONE:
  166.                 /*printf("Action none!\n");*/
  167.                 break;
  168.             case A_CIRCLEF:
  169.             case A_CIRCLE:
  170.                 px=CX(P_Tab[0]);
  171.                 py=CY(P_Tab[1]);
  172.                 rx=CX(P_Tab[2]+P_Tab[0])-px;
  173.                 if(Nb_Arg==3){
  174.                     ry=CY(P_Tab[2]+P_Tab[0])-CY(P_Tab[0]);
  175.                 }else{
  176.                     ry=CY(P_Tab[3]+P_Tab[1])-CY(P_Tab[1]);
  177.                 }
  178.                 if(Token==A_CIRCLE){
  179.                     DrawEllipse(rp,px,py,rx,ry);
  180.                 }else{
  181.                     SetDrMd(rp,JAM2);
  182.                     AreaEllipse(rp,px,py,rx,ry);
  183.                     AreaEnd(rp);
  184.                 }
  185.                 break;
  186.             case A_APEN:
  187.                 SetAPen(rp,Tab_Color[P_Tab[0]].Num_Col);
  188.                 break;
  189.             case A_OPEN:
  190.                 SetOPen(rp,Tab_Color[P_Tab[0]].Num_Col);
  191.                 break;
  192.             case A_RECT:
  193.                 Move(rp,CX(P_Tab[0]),CY(P_Tab[1]));
  194.                 Draw(rp,CX(P_Tab[2]),CY(P_Tab[1]));
  195.                 Draw(rp,CX(P_Tab[2]),CY(P_Tab[3]));
  196.                 Draw(rp,CX(P_Tab[0]),CY(P_Tab[3]));
  197.                 Draw(rp,CX(P_Tab[0]),CY(P_Tab[1]));
  198.                 break;
  199.             case A_RECTF:
  200.                 RectFill(rp,            CX(P_Tab[0]),
  201.                                 CY(P_Tab[1]),
  202.                                 CX(P_Tab[2]),
  203.                                 CY(P_Tab[3])
  204.                                 );
  205.                 break;
  206.             case A_POLY:
  207.                 Move(rp,CX(P_Tab[0]),CY(P_Tab[1]));
  208.                 for(i=2;i<Nb_Arg;i+=2){
  209.                     Draw(rp,CX(P_Tab[i]),CY(P_Tab[i+1]));
  210.                 }
  211.                 break;
  212.  
  213.             case A_POLYF:
  214.                 AreaMove(rp,CX(P_Tab[0]),CY(P_Tab[1]));
  215.                 for(i=2;i<Nb_Arg;i+=2){
  216.                     AreaDraw(rp,CX(P_Tab[i]),CY(P_Tab[i+1]));
  217.                 }
  218.                 AreaEnd(rp);
  219.                 break;
  220.  
  221.             case A_FIVE_STAR:
  222.                 if(Nb_Arg==2){
  223.                     Sx=100;
  224.                     Sy=100;
  225.                 }else{
  226.                     Sx=P_Tab[2];
  227.                     Sy=Sx;
  228.                     if(Nb_Arg==4){
  229.                         Angle=((float)P_Tab[3])*2*PI/360.0;
  230.                     }else{
  231.                         Angle=0;
  232.                     }
  233.                 }
  234.                 i=0;
  235.                 while(Tab_Star[i][0]!=-1){
  236.                     px=Tab_Star[i][0];
  237.                     py=Tab_Star[i][1];
  238.                     if(Angle!=0){
  239.                         T3D_2D_Rot(Angle,&px,&py);
  240.                     }
  241.                     px=CX(P_Tab[0]+(px*Sx)/100);
  242.                     py=CY(P_Tab[1]-(py*Sy)/100);
  243.                     if(i==0){
  244.                         AreaMove(rp,px,py);
  245.                     }else{
  246.                         AreaDraw(rp,px,py);
  247.                     }
  248.                     i++;
  249.                 }
  250.                 AreaEnd(rp);
  251.                 break;
  252.  
  253.  
  254.             case A_STAR_MULT:
  255.                 Nb=P_Tab[2];
  256.                 Sext=P_Tab[3];
  257.                 Sint=P_Tab[4];
  258.  
  259.                 for(i=0;i<Nb;i++){
  260.                     Angle=(i*2-1)*2*PI/(Nb*2);
  261.                     px=0;
  262.                     py=-Sint;
  263.                     T3D_2D_Rot(Angle,&px,&py);
  264.                     if(i==0){
  265.                         AreaMove(rp,CX(P_Tab[0]+px),CY(P_Tab[1]+py));
  266.                     }else{
  267.                         AreaDraw(rp,CX(P_Tab[0]+px),CY(P_Tab[1]+py));
  268.                     }
  269.  
  270.                     Angle=(i)*2*PI/(Nb);
  271.                     px=0;
  272.                     py=-Sext;
  273.                     T3D_2D_Rot(Angle,&px,&py);
  274.                     AreaDraw(rp,CX(P_Tab[0]+px),CY(P_Tab[1]+py));
  275.  
  276.                 }
  277.                 AreaEnd(rp);
  278.                 break;
  279.  
  280.             case A_BACKG:
  281.                 SetAPen(rp,Tab_Color[P_Tab[0]].Num_Col);
  282.                 RectFill(rp,glob_clip.minx,
  283.                     glob_clip.miny,
  284.                     glob_clip.maxx,
  285.                     glob_clip.maxy);
  286.  
  287.                 break;
  288.             case A_END:
  289.                 Flg_End=TRUE;
  290.                 break;
  291.             default:
  292.                 /*printf("Unknow action:%d\n",Token); */
  293.                 break;
  294.         }
  295.         P_Tab+=Nb_Arg;
  296.         /* On se previent d'un eventuel probleme de bouclage */
  297.         i++;
  298.         if(i>1000)Flg_End=TRUE;
  299.     }
  300.     SetAPen(rp,0);
  301.     Move(rp,CX(0),CY(0));
  302.     Draw(rp,CX(0),CY(FLAG_Y));
  303.     Draw(rp,CX(FLAG_X),CY(FLAG_Y));
  304.     Draw(rp,CX(FLAG_X),CY(0));
  305.     Draw(rp,CX(0),CY(0));
  306.     
  307. }
  308.  
  309. /**************************************************************************/
  310.  
  311. int Find_Token(char *p_token)
  312. {
  313.     int i;
  314.  
  315.     i=0;
  316.     while((i<MAX_TOKEN)&&(stricmp(Tab_Token[i].Name, p_token )!=0)){
  317.         i++;
  318.     }
  319.     if(i==MAX_TOKEN)i=0;
  320. #ifdef DEBUG_LEV2
  321.     printf("Token:%d\n",i);
  322. #endif
  323.     return(i);
  324. }
  325.  
  326. /**************************************************************************/
  327.  
  328. BOOL    Get_Elem(char **pp,char *p_elem)
  329. {
  330.     int i=0;
  331.     char    *Deb;
  332.  
  333.     Deb=p_elem;
  334.     *p_elem=0;
  335.     while((*(*pp) !=0)&&(*(*pp)!=',')&&(**pp!='\n')&&(*(*pp)!=' ')
  336.             &&(*(*pp)!=0x09)){
  337.         *p_elem=**pp;
  338.         p_elem++;
  339.         (*pp)++;
  340.         i++;
  341.         *p_elem=0;
  342.     }
  343.     while((*(*pp)==',')||(**pp==0x09)||(*(*pp)==' ')){
  344.         (*pp)++;
  345.     }
  346.  
  347.     if(i==0){
  348.         return(FALSE);
  349.     }else{
  350.         return(TRUE);
  351.     }
  352.  
  353. }
  354. /**************************************************************************/
  355. int Find_Color(char *P_Buf)
  356. {
  357.     int i;
  358.     struct  Def_Color   *P_Temp;
  359.  
  360.     P_Temp=Tab_Color;
  361.     i=0;
  362.     while((P_Temp->r!=-1)&&(stricmp(P_Temp->Name, P_Buf )!=0)){
  363.         P_Temp++;
  364.         i++;
  365.         
  366.     }
  367.     if(P_Temp->r==-1)i=0;
  368. #ifdef DEBUG_LEV1
  369.     printf("Color:%s %d\n",P_Buf,i);
  370. #endif
  371.     return(i);
  372. }
  373.  
  374. void    InitColors(struct ColorMap *cm)
  375. {
  376.     int i;
  377.     struct  Def_Color   *P_Temp;
  378. /*    struct  TagItem My_Tab[2]={
  379.                 OBP_FailIfBad,FALSE,TAG_END};*/
  380.  
  381.     LONG    Num_Col;
  382.  
  383.     P_Temp=Tab_Color;
  384.     if(GfxBase->LibNode.lib_Version>=39){
  385.  
  386.         i=0;
  387.         while(P_Temp->r!=-1){
  388.             Num_Col=FindColor( cm,
  389.                     P_Temp->r<<28,
  390.                     P_Temp->v<<28,
  391.                     P_Temp->b<<28,
  392.                     -1
  393.                     );
  394.             P_Temp->Num_Col=Num_Col;
  395.             P_Temp++;
  396.         }
  397.     }else{
  398.         /*printf("Fct a ecrire pour WB 2.0!\n");*/
  399.     }
  400. }
  401.  
  402. /**************************************************************************/
  403.  
  404. BOOL    read_one(FILE *fp,int   **p_current)
  405. {
  406.     BOOL    Flg_Cont,Encore;
  407.     int Token;
  408.     char    Buffer_Temp[BUFFER_SIZE],*P_Buf;
  409.     char    Buffer_Token[BUFFER_SIZE];
  410.     int Data,Nb_Var;
  411.     int *p_avant;
  412.  
  413.     Flg_Cont=TRUE;
  414.  
  415.     /* On lit une ligne, tout en supprremant les lignes */
  416.     /* de commentaires qui commencent par un ;      */
  417.     do{
  418.         P_Buf = fgets(Buffer_Temp,BUFFER_SIZE,fp);
  419.     }while((P_Buf!=NULL)&&(P_Buf[0]==';'));
  420.  
  421.     if(P_Buf==NULL){
  422.         Flg_Cont=FALSE;
  423.     }else{
  424. #ifdef DEBUG_LEV1
  425.         printf("Ligne:%s\n",P_Buf);
  426. #endif
  427.         Encore=Get_Elem(&P_Buf,Buffer_Token);
  428.         Flg_Cont=Encore;
  429.         if(Encore){
  430.             p_avant=*p_current;
  431.             (*p_current)++;
  432.             Token=Find_Token(Buffer_Token);
  433.             Nb_Var=0;
  434.             while(Encore){
  435.                 Encore=Get_Elem(&P_Buf,Buffer_Token);
  436.                 if(Encore){
  437. #ifdef DEBUG_LEV2
  438.                     printf("Elem:%s\n",Buffer_Token);
  439. #endif
  440.                     if(isalpha(Buffer_Token[0])){
  441.                         Data=Find_Color(Buffer_Token);
  442.                     }else{
  443.                         Data=atoi(Buffer_Token);
  444.  
  445.                     }
  446.                     *(*p_current)=Data;
  447.                     (*p_current)++;
  448.                     Nb_Var++;
  449.                 }
  450.             }
  451.             if((Nb_Var < Tab_Token[Token].Nb_Min_Arg)||
  452.                (Nb_Var > Tab_Token[Token].Nb_Max_Arg)){
  453.                 /*printf("Erreur sur le nombre de parametres!\n");*/
  454.             }else{
  455. #ifdef DEBUG_LEV1
  456.                 printf("Token:%d Nb_Var:%d\n",Token,Nb_Var);
  457. #endif
  458.             }
  459.             *p_avant=Token + (Nb_Var<<8);
  460.         }
  461.  
  462.     }
  463.     **p_current=A_END;
  464.     return(Flg_Cont);
  465. }
  466.  
  467. BOOL Flag_Read(char *p_filename,int **PP_Tab_Drapeau)
  468. {
  469.     BOOL    Flg_Cont;
  470.     FILE    *fp;
  471.     int *P_Tab;
  472.  
  473.     *PP_Tab_Drapeau=malloc(sizeof(int)*(MAX_ELEMS));
  474.  
  475.     fp=fopen(p_filename,"r");
  476.     if(fp!=NULL){
  477.         P_Tab=*PP_Tab_Drapeau;
  478.         Flg_Cont=TRUE;
  479.         while(Flg_Cont==TRUE){
  480.             Flg_Cont=read_one(fp,&P_Tab);
  481.         }
  482.         fclose(fp);
  483.         return(TRUE);
  484.     }else{
  485. #ifdef DEBUG_LEV1
  486.         printf("ERREUR: Ouverture fichier drapeau:%s!\n",p_filename);
  487. #endif
  488.         return(FALSE);
  489.     }
  490. }
  491.  
  492.